package leetcode.editor.cn.q1_300.q50;

import java.util.Arrays;

/**
 * @author qidi
 * @date 2021-12-13 15:35:23
 */
class Q0066_PlusOne {

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int[] plusOne(int[] digits) {
            if (digits.length == 0) return new int[]{1};

            int tmp = 1;
            for (int i = digits.length - 1; i >= 0; i--) {
                int num = digits[i] + tmp;
                if (num > 9) {
                    tmp = 1;
                    digits[i] = num - 10;
                } else {
                    tmp = 0;
                    digits[i] = num;
                    break;
                }
            }
            if (tmp == 1) {
                int[] arr = new int[digits.length + 1];
                arr[0] = 1;
                System.arraycopy(digits, 0, arr, 1, digits.length);
                return arr;
            }
            return digits;
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)


    public static void main(String[] args) {
        Solution solution = new Q0066_PlusOne().new Solution();
        System.out.println(Arrays.toString(solution.plusOne(new int[]{9, 9, 9})));
        System.out.println(Arrays.toString(solution.plusOne(new int[]{4, 3, 2, 1})));
    }
}